package com.cskaoyan.app.wxservice.impl;

import com.cskaoyan.app.bean.common.BasePageInfo;
import com.cskaoyan.app.bean.common.CommonData;
import com.cskaoyan.app.bean.po.MarketGoods;
import com.cskaoyan.app.bean.po.MarketTopic;
import com.cskaoyan.app.bean.po.MarketTopicExample;
import com.cskaoyan.app.mapper.MarketGoodsMapper;
import com.cskaoyan.app.mapper.MarketTopicMapper;
import com.cskaoyan.app.bean.vo.wx.TopicDetailVo;
import com.cskaoyan.app.wxservice.WxTopicService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;


@Service
public class WxTopicServiceImpl implements WxTopicService {
    @Autowired
    MarketTopicMapper marketTopicMapper;
    @Autowired
    MarketGoodsMapper marketGoodsMapper;

    /**
     * @param id 前端传入的key=value -->  topic(id 主键)
     * @return TopicDetailVo   两个成员变量
     * MarketTopic topic;
     * List<MarketGoods> goods;
     */
    @Override
    public TopicDetailVo detail(Integer id) {
        //两张表的对应关系
        // topic(goods[])  -->  goods(id 主键)
        MarketTopic topic = marketTopicMapper.selectByPrimaryKey(id);
        Integer[] goods = topic.getGoods();
        //根据goodsId[]   得到goods信息[]  封装为List
        LinkedList<MarketGoods> marketGoodsList = new LinkedList<>();
        for (Integer good : goods) {
            MarketGoods marketGoods = marketGoodsMapper.selectByPrimaryKey(good);
            marketGoodsList.add(marketGoods);
        }
        //新建返回值对象  setValue  两个成员变量
        TopicDetailVo topicDetailVo = new TopicDetailVo();
        topicDetailVo.setTopic(topic);
        topicDetailVo.setGoods(marketGoodsList);
        return topicDetailVo;
    }

    @Override
    public CommonData<MarketTopic> related(Integer id) {
        //这边未使用分页功能 故使用固定值
        CommonData<MarketTopic> commonData = new CommonData<>();
        commonData.setLimit(4);
        commonData.setPage(1);
        commonData.setPages(6);
        commonData.setTotal(21);
        PageHelper.startPage(1, 4);
        //此处为关联项  即 专题推荐  点开商品之后 下方推荐 要排除此商品进行推荐
        MarketTopicExample example = new MarketTopicExample();
        MarketTopicExample.Criteria criteria = example.createCriteria();
        criteria.andIdNotEqualTo(id);
        //逻辑删除 判断 未被删除才会显示
        criteria.andDeletedEqualTo(false);
        List<MarketTopic> marketTopics = marketTopicMapper.selectByExample(example);
        commonData.setList(marketTopics);
        return commonData;


    }

    @Override
    public CommonData<MarketTopic> list(BasePageInfo basePageInfo) {

        //开启 分页  之后 只需要查询所有数据就可以了
        if (basePageInfo.getPage() != null && basePageInfo.getLimit() != null) {
            PageHelper.startPage(basePageInfo.getPage(), basePageInfo.getLimit());
        }
        //获得list
        MarketTopicExample example = new MarketTopicExample();
        MarketTopicExample.Criteria criteria = example.createCriteria();
        //逻辑删除 判断是否被删除
        criteria.andDeletedEqualTo(false);
        example.setOrderByClause("id desc");
        List<MarketTopic> list = marketTopicMapper.selectByExample(example);
        //需要一个list
        PageInfo<MarketTopic> pageInfo = new PageInfo<>(list);
        //需要pageInfo
        CommonData<MarketTopic> data = CommonData.data(pageInfo);
        return data;
    }
}
